from urllib import parse
from Education.model.session import session
from Education.util.analyzer.getNetPostKey import get_viewstate_eventvalidation


def final_scores_spider(user, name):
    url = "http://124.160.107.91:9090/xscjcx_dq.aspx"

    params = {
        'xh': user,
        'xm': name,
        'gnmkdm': 'N121605',
    }

    headers = {
        'Referer': "http://124.160.107.91:9090/xs_main.aspx?xh=" + user,
        'Content-Type': 'application/x-www-form-urlencoded',
    }

    dict01 = {
        '__EVENTTARGET': '',
        '__EVENTARGUMENT': '',
        '__LASTFOCUS': '',
        'ddlxn': '2017-2018',
        'ddlxq': '2',
        'btnCx': ' 查  询 ',
    }

    # 获取viewstate和eventvalidation值
    response = session.get(url=url, headers=headers, params=params)

    dict01.update(get_viewstate_eventvalidation(response))
    data = parse.urlencode(dict01)

    response = session.post(url=url, data=data, headers=headers, params=params)
    html = response.text
    print(html)
    return html
